home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 5 / Example 5.3 / mouse.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-07-12  |  5.0 KB  |  205 lines

  1. #include "mouse.h"
  2.  
  3. //////////////////////////////////////////////////////////////////////////
  4. //                        RAY                                                //
  5. //////////////////////////////////////////////////////////////////////////
  6.  
  7. RAY::RAY()
  8. {
  9.     org = dir = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  10. }
  11.  
  12. RAY::RAY(D3DXVECTOR3 o, D3DXVECTOR3 d)
  13. {
  14.     org = o;
  15.     dir = d;
  16. }
  17.  
  18. float RAY::Intersect(MESHINSTANCE iMesh)
  19. {
  20.     if(iMesh.m_pMesh == NULL || iMesh.m_pMesh->m_pMesh == NULL)return -1.0f;
  21.  
  22.     // Collect only the closest intersection
  23.     BOOL hit;
  24.     DWORD dwFace;
  25.     float hitU, hitV, dist;
  26.     D3DXIntersect(iMesh.m_pMesh->m_pMesh, &org, &dir, &hit, &dwFace, &hitU, &hitV, &dist, NULL, NULL);
  27.  
  28.     if(hit) return dist;
  29.     else return -1.0f;
  30. }
  31.  
  32. float RAY::Intersect(BBOX bBox)
  33. {
  34.     if(D3DXBoxBoundProbe(&bBox.min, &bBox.max, &org, &dir))
  35.         return D3DXVec3Length(&(((bBox.min + bBox.max) / 2.0f) - org));
  36.     else return -1.0f;
  37. }
  38.  
  39. float RAY::Intersect(BSPHERE bSphere)
  40. {
  41.     if(D3DXSphereBoundProbe(&bSphere.center, bSphere.radius, &org, &dir))
  42.         return D3DXVec3Length(&(bSphere.center - org));
  43.     else return -1.0f;
  44. }
  45.  
  46. //////////////////////////////////////////////////////////////////////////
  47. //                        MOUSE                                            //
  48. //////////////////////////////////////////////////////////////////////////
  49.  
  50. MOUSE::MOUSE()
  51. {
  52.     m_type = 0;
  53.     m_pMouseTexture = NULL;
  54.     m_pMouseDevice = NULL;
  55.     m_fSpeed = 1.5f;
  56. }
  57.  
  58. MOUSE::~MOUSE()
  59. {
  60.     if(m_pMouseDevice != NULL)
  61.         m_pMouseDevice->Release();
  62.  
  63.     if(m_pMouseTexture != NULL)
  64.         m_pMouseTexture->Release();
  65. }
  66.  
  67. void MOUSE::InitMouse(IDirect3DDevice9* Dev, HWND wnd)
  68. {
  69.     try
  70.     {
  71.         m_pDevice = Dev;
  72.  
  73.         //Load texture and init sprite
  74.         D3DXCreateTextureFromFile(m_pDevice, "cursor/cursor.dds", &m_pMouseTexture);
  75.         D3DXCreateSprite(m_pDevice, &m_pSprite);
  76.  
  77.         //Get directinput object
  78.         LPDIRECTINPUT8 directInput = NULL;
  79.  
  80.         DirectInput8Create(GetModuleHandle(NULL),    // Retrieves the application handle
  81.                            0x0800,                    // v.8.0    
  82.                            IID_IDirectInput8,        // Interface ID
  83.                            (VOID**)&directInput,    // Our DirectInput Object
  84.                            NULL);
  85.  
  86.         //Acquire Default System mouse
  87.         directInput->CreateDevice(GUID_SysMouse, &m_pMouseDevice, NULL);        
  88.         m_pMouseDevice->SetDataFormat(&c_dfDIMouse);
  89.         m_pMouseDevice->SetCooperativeLevel(wnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND);
  90.         m_pMouseDevice->Acquire();            
  91.  
  92.         //Get viewport size and init mouse location
  93.         D3DVIEWPORT9 v;
  94.         m_pDevice->GetViewport(&v);
  95.         m_viewport.left = v.X;
  96.         m_viewport.top = v.Y;
  97.         m_viewport.right = v.X + v.Width;
  98.         m_viewport.bottom = v.Y + v.Height;
  99.  
  100.         x = v.X + v.Width / 2.0f;
  101.         y = v.Y + v.Height / 2.0f;
  102.  
  103.         //Release Direct Input Object
  104.         directInput->Release();
  105.     }
  106.     catch(...)
  107.     {
  108.         debug.Print("Error in MOUSE::InitMouse()");
  109.     }    
  110. }
  111.  
  112. bool MOUSE::ClickLeft()
  113.     return m_mouseState.rgbButtons[0];
  114. }
  115.  
  116. bool MOUSE::ClickRight()
  117. {
  118.     return m_mouseState.rgbButtons[1];
  119. }
  120.  
  121. bool MOUSE::WheelUp()
  122. {
  123.     return m_mouseState.lZ > 0.0f;
  124. }
  125.  
  126. bool MOUSE::WheelDown()
  127. {
  128.     return m_mouseState.lZ < 0.0f;
  129. }
  130.  
  131. bool MOUSE::Over(RECT dest)
  132. {
  133.     if(x < dest.left || x > dest.right)return false;
  134.     if(y < dest.top || y > dest.bottom)return false;
  135.     return true;
  136. }
  137.  
  138. bool MOUSE::PressInRect(RECT dest)
  139. {
  140.     return ClickLeft() && Over(dest);
  141. }
  142.  
  143. void MOUSE::Update()
  144. {
  145.     //Retrieve mouse state
  146.     ZeroMemory(&m_mouseState, sizeof(DIMOUSESTATE));
  147.     m_pMouseDevice->GetDeviceState(sizeof(DIMOUSESTATE), &m_mouseState);
  148.  
  149.     //Update pointer
  150.     x += m_mouseState.lX * m_fSpeed;
  151.     y += m_mouseState.lY * m_fSpeed;
  152.  
  153.     //Keep mouse pointer within window
  154.     if(x < m_viewport.left)        x = m_viewport.left;
  155.     if(y < m_viewport.top)        y = m_viewport.top;
  156.     if(x > m_viewport.right)    x = m_viewport.right;
  157.     if(y > m_viewport.bottom)    y = m_viewport.bottom;
  158. }
  159.  
  160. void MOUSE::Paint()
  161. {    
  162.     if(m_pMouseTexture != NULL)
  163.     {
  164.         RECT src[] = {{0,0,20,20}, {0,20,20,40}, {20,20,40,40}, {0,40,20,60}, {20,40,40,60}};
  165.  
  166.         m_pSprite->Begin(D3DXSPRITE_ALPHABLEND);
  167.         m_pSprite->Draw(m_pMouseTexture, &src[m_type],    NULL, &D3DXVECTOR3(x, y, 0.0f), 0xffffffff);
  168.         m_pSprite->End();
  169.     }    
  170. }
  171.  
  172. RAY MOUSE::GetRay()
  173. {
  174.     try
  175.     {
  176.         D3DXMATRIX projectionMatrix, viewMatrix, worldViewInverse, worldMatrix;
  177.         
  178.         m_pDevice->GetTransform(D3DTS_PROJECTION, &projectionMatrix);
  179.         m_pDevice->GetTransform(D3DTS_VIEW, &viewMatrix);
  180.         m_pDevice->GetTransform(D3DTS_WORLD, &worldMatrix);
  181.  
  182.         float width = m_viewport.right - m_viewport.left;
  183.         float height = m_viewport.bottom - m_viewport.top;
  184.  
  185.         float angle_x = (((2.0f * x) / width) - 1.0f) / projectionMatrix(0,0);
  186.         float angle_y = (((-2.0f * y) / height) + 1.0f) / projectionMatrix(1,1); 
  187.         
  188.         RAY ray;
  189.         ray.org = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  190.         ray.dir = D3DXVECTOR3(angle_x, angle_y, 1.0f);
  191.  
  192.         D3DXMATRIX m = worldMatrix * viewMatrix;
  193.         D3DXMatrixInverse(&worldViewInverse, 0, &m);
  194.         D3DXVec3TransformCoord(&ray.org, &ray.org, &worldViewInverse);
  195.         D3DXVec3TransformNormal(&ray.dir, &ray.dir, &worldViewInverse);
  196.         D3DXVec3Normalize(&ray.dir, &ray.dir);
  197.  
  198.         return ray;
  199.     }
  200.     catch(...)
  201.     {
  202.         debug.Print("Error in MOUSE::GetRay()");
  203.     }
  204. }